﻿#include "NpcBaseDescEx.h"

#include "DescStore/FunctionNpcDesc.h"
#include "DescStore/NpcLocationDesc.h"
#include "DescStore/NpcSpawnDesc.h"

NpcBaseDescEx::NpcBaseDescEx()
{
	if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode()) {
		CreateInit();
	}
	else {
		ResumeInit();
	}
}

NpcBaseDescEx::~NpcBaseDescEx()
{
}

int NpcBaseDescEx::CreateInit()
{
	return 0;
}

int NpcBaseDescEx::ResumeInit()
{
	return 0;
}

int NpcBaseDescEx::Load()
{
	return 0;
}

int NpcBaseDescEx::CheckWhenAllDataLoaded()
{
    for (int i = 0; i < NpcSpawnDesc::Instance()->GetResNum(); i++)
    {
        auto pDesc = NpcSpawnDesc::Instance()->GetDescByIndex(i);
        CHECK_NULL(0, pDesc);

        if (pDesc->location_group_id <= 0) continue;

        auto vec = NpcLocationDesc::Instance()->GetDescByLocationGroupId(pDesc->location_group_id);
        CHECK_EXPR(vec.size() > 0 && vec.size() <= MAX_LOCATION_NUM, -1, "NpcSpawnDesc id:{} location_group_id:{} not found in NpcLocationDesc", pDesc->id, pDesc->location_group_id);

        auto vecGroup = FunctionNpcDesc::Instance()->GetDescByGroupId(pDesc->npc_group);
        CHECK_EXPR(vecGroup.size() > 0, -1, "NpcSpawnDesc id:{} npc_group:{} not found in FunctionNpcDesc", pDesc->id, pDesc->npc_group);

        if (pDesc->start_date.size() > 0)
        {
            time_t utc_start_time = 0;
            if (NFTimeUtility::DateStrToLocalTime(pDesc->start_date.c_str(), &utc_start_time) != 0)
            {
                LOG_ERR(0, -1, "NpcSpawnDesc id:{} start_date:{} format error, should be YYYY-MM-DD", pDesc->id, pDesc->start_date.ToString());
            }
            pDesc->utc_start_time = utc_start_time;
        }

        if (pDesc->end_date.size() > 0)
        {
            time_t utc_end_time = 0;
            if (NFTimeUtility::DateStrToLocalTime(pDesc->end_date.c_str(), &utc_end_time) != 0)
            {
                LOG_ERR(0, -1, "NpcSpawnDesc id:{} end_date:{} format error, should be YYYY-MM-DD", pDesc->id, pDesc->end_date.ToString());
            }
            pDesc->utc_end_time = utc_end_time;
        }

        if (pDesc->start_time.size() > 0)
        {
            time_t utc_start_daytime = 0;
            if (NFTimeUtility::GetTimeSec(pDesc->start_time.c_str(), &utc_start_daytime) != 0)
            {
                LOG_ERR(0, -1, "NpcSpawnDesc id:{} start_time:{} format error, should be HOUR:MIN:SEC", pDesc->id, pDesc->start_time.ToString());
            }
            pDesc->utc_start_daytime = utc_start_daytime;
        }

        if (pDesc->end_time.size() > 0)
        {
            time_t utc_end_daytime = 0;
            if (NFTimeUtility::GetTimeSec(pDesc->end_time.c_str(), &utc_end_daytime) != 0)
            {
                LOG_ERR(0, -1, "NpcSpawnDesc id:{} end_time:{} format error, should be HOUR:MIN:SEC", pDesc->id, pDesc->end_time.ToString());
            }
            pDesc->utc_end_daytime = utc_end_daytime;
        }
    }

	return 0;
}

